﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevHelp;

namespace File_ProcessorApp
{
    public partial class 编码转文件 : Form
    {
        StreamHelper streamHelper = new StreamHelper();
        static FileType fileType = new FileType();
        private List<FileType> fileTypeList = fileType.SetFileType();

        public 编码转文件()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            cboFileType.DataSource = fileType.SetFileType();
            cboFileType.ValueMember = "Id";
            cboFileType.DisplayMember = "Name";
        }

        /// <summary>
        /// 保存base64到指定路径下
        /// </summary>
        /// <param name="base64Str"></param>
        /// <param name="filePath"></param>
        private void SaveFile(FileType selectedFileType, string localFilePath)
        {
            try
            {
                switch (selectedFileType.Name)
                {
                    case "Image":
                        streamHelper.StringToImage(txtBase64.Text, localFilePath);
                        break;
                    default:
                        streamHelper.Base64ToFile(txtBase64.Text, localFilePath);
                        break;
                }

                MessageBox.Show("文件保存成功");
            }
            catch (Exception exception)
            {
                if (exception.Message.Contains("字符数组或字符串的长度无效"))
                {
                    MessageBox.Show("txtBase64的字符数过多");
                }
                else
                {
                    MessageBox.Show(exception.Message);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string localFilePath = "";
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            var selectedFileType = fileTypeList.Where(a => a.Id.Equals(cboFileType.SelectedValue)).FirstOrDefault();
            //设置文件类型
            saveFileDialog.Filter = selectedFileType?.FileTypeFilter;
            saveFileDialog.FilterIndex = 1;
            //保存对话框是否记忆上次打开的目录
            saveFileDialog.RestoreDirectory = true;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                localFilePath = saveFileDialog.FileName.ToString();
            }

            if (string.IsNullOrEmpty(txtBase64.Text))
            {
                MessageBox.Show("Base64编码不可为空");
            }
            else if (string.IsNullOrEmpty(localFilePath))
            {
                MessageBox.Show("请选择保存路径");
            }
            else
            {
                SaveFile(selectedFileType, localFilePath);
            }
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            string localFilePath = "";
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = false;
            openFileDialog.Title = "请选择文件";
            //设置要选择的文件的类型
            openFileDialog.Filter = fileTypeList.Where(a => a.Id.Equals(cboFileType.SelectedValue)).FirstOrDefault()
                ?.FileTypeFilter;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                localFilePath = openFileDialog.FileName; //返回文件的完整路径
            }

            if (string.IsNullOrEmpty(localFilePath))
            {
                MessageBox.Show("请选择文件路径");
            }
            else
            {
                var bytes = streamHelper.FileTobyte(localFilePath);
                string bytesBase64 = Convert.ToBase64String(bytes);
                if (txtBase64.MaxLength < bytesBase64.Length)
                {
                    MessageBox.Show("选择的文件过大，已超过文本框的最大长度");
                }
                else
                {
                    txtBase64.Text = bytesBase64;
                }
            }
        }
    }
}